<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Template Resources</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="resources.html" title="Chapter 16. Resources">
<link rel="prev" href="resources.extends.html" title="Extends Template Resources">
<link rel="next" href="advanced.features.html" title="Chapter 17. Advanced Features">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Custom Template Resources</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="resources.extends.html">Prev</a> </td>
<th width="60%" align="center">Chapter 16. Resources</th>
<td width="20%" align="right"> <a accesskey="n" href="advanced.features.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Custom Template Resources">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="resources.custom"></a>Custom Template Resources</h2></div></div></div>
<p>
  You can retrieve templates using whatever possible source you can
  access with PHP: databases, sockets, files, etc. You do this
  by writing resource plugin functions and registering them with
  Smarty.
 </p>
<p>
  See <a class="link" href="plugins.resources.html" title="Resources">resource plugins</a>
  section for more information on the functions you are supposed
  to provide.
 </p>
<div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
   Note that you cannot override the built-in
   <code class="literal">file:</code> resource, but you can provide a resource
   that fetches templates from the file system in some other way by
   registering under another resource name.
  </p>
</div>
<div class="example">
<a name="id456130"></a><p class="title"><b>Example 16.10. Using custom resources</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;?php

/**
* MySQL Resource
*
* Resource Implementation based on the Custom API to use
* MySQL as the storage resource for Smarty's templates and configs.
*
* Table definition:
* &lt;pre&gt;CREATE TABLE IF NOT EXISTS `templates` (
*   `name` varchar(100) NOT NULL,
*   `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
*   `source` text,
*   PRIMARY KEY (`name`)
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8;&lt;/pre&gt;
*
* Demo data:
* &lt;pre&gt;INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');&lt;/pre&gt;
*
* @package Resource-examples
* @author Rodney Rehm
*/
class Smarty_Resource_Mysql extends Smarty_Resource_Custom {
 // PDO instance
 protected $db;
 // prepared fetch() statement
 protected $fetch;
 // prepared fetchTimestamp() statement
 protected $mtime;

 public function __construct() {
     try {
         $this-&gt;db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty");
     } catch (PDOException $e) {
         throw new SmartyException('Mysql Resource failed: ' . $e-&gt;getMessage());
     }
     $this-&gt;fetch = $this-&gt;db-&gt;prepare('SELECT modified, source FROM templates WHERE name = :name');
     $this-&gt;mtime = $this-&gt;db-&gt;prepare('SELECT modified FROM templates WHERE name = :name');
 }
 
 /**
  * Fetch a template and its modification time from database
  *
  * @param string $name template name
  * @param string $source template source
  * @param integer $mtime template modification timestamp (epoch)
  * @return void
  */
 protected function fetch($name, &amp;$source, &amp;$mtime)
 {
     $this-&gt;fetch-&gt;execute(array('name' =&gt; $name));
     $row = $this-&gt;fetch-&gt;fetch();
     $this-&gt;fetch-&gt;closeCursor();
     if ($row) {
         $source = $row['source'];
         $mtime = strtotime($row['modified']);
     } else {
         $source = null;
         $mtime = null;
     }
 }
 
 /**
  * Fetch a template's modification time from database
  *
  * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source.
  * @param string $name template name
  * @return integer timestamp (epoch) the template was modified
  */
 protected function fetchTimestamp($name) {
     $this-&gt;mtime-&gt;execute(array('name' =&gt; $name));
     $mtime = $this-&gt;mtime-&gt;fetchColumn();
     $this-&gt;mtime-&gt;closeCursor();
     return strtotime($mtime);
 }
}


require_once 'libs/Smarty.class.php';
$smarty = new Smarty();
$smarty-&gt;registerResource('mysql', new Smarty_Resource_Mysql());

// using resource from php script
$smarty-&gt;display("mysql:index.tpl");
?&gt;

  </pre>
<p>
   And from within Smarty template:
  </p>
<pre class="programlisting">

{include file='mysql:extras/navigation.tpl'}

  </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="resources.extends.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="resources.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="advanced.features.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Extends Template Resources </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Chapter 17. Advanced Features</td>
</tr>
</table>
</div>
</body>
</html>
